package cn.com.xky.springbootjpa.service.impl;

import cn.com.xky.springbootjpa.dao.IUserDao;
import cn.com.xky.springbootjpa.domain.User;
import cn.com.xky.springbootjpa.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by EKEYXIA on 2017/8/14.
 */
@Service
public class UserServiceImp implements IUserService {

    @Autowired
    private IUserDao userDao;

    @Override
    public void add(User user) {
        userDao.save(user);
    }

    @Override
    public void delete(Integer id) {
        userDao.delete(Long.valueOf(id));
    }

    @Override
    public void update(Integer id, User user) {
        User newUser = userDao.findOne(Long.valueOf(id));
        newUser.setName(user.getName());
        newUser.setAge(user.getAge());
        userDao.saveAndFlush(newUser);
    }

    @Override
    public List<User> getUsers() {
        return userDao.findAll();
    }

    @Override
    public User getOne(Integer id) {
        return userDao.findOne(Long.valueOf(id));
    }
}
